import Cookies from 'js-cookie'
import { ElckerEncrypt, ElckerDecrypt } from '@/plugins/crypto/ElckerCrypto'

// Cookie的key值
export const ElckerTokenKey = {
    accessTokenKey: '_atk', // 访问令牌在cookie的key值 
    refreshTokenKey: '_rtk', // 刷新令牌在cookie的key值 
    userInfoKey: '_uio',
    isCome: '_ppo'
}

class CookieClass {
    private domain: any;
    private expireTime: any;
    constructor() {
        this.domain = process.env.VUE_APP_COOKIENAME // 域名
        this.expireTime = 1 // 1 天
    }

    set(key: any, value: any, expires?: any, path = '/') {
        CookieClass.checkKey(key);
        Cookies.set(key, ElckerEncrypt(value), { expires: expires || this.expireTime, path: path, domain: this.domain })
    }

    get(key: any) {
        CookieClass.checkKey(key)
        return Cookies.get(key) == undefined || Cookies.get(key) == null || "" == Cookies.get(key) ? null : ElckerDecrypt(Cookies.get(key));
    }

    remove(key: any, path = '/') {
        CookieClass.checkKey(key)
        Cookies.remove(key, { path: path, domain: this.domain })
    }

    geteAll() {
        Cookies.get();
    }

    static checkKey(key: any) {
        if (!key) {
            throw new Error('没有找到key。');
        }
        if (typeof key === 'object') {
            throw new Error('key不能是一个对象。');
        }
    }
}

// 导出
export const ElckerCookie = new CookieClass()
